<template>
  <base-layout
    :dialogVisible="dialogVisible"
    :dialogTitle="dialogTitle"
    @dialog-close="dialogClose"
    :visibleContent3="true"
  >
    <template #searchPanel>
      <el-form
        :inline="true"
        ref="fromdata"
        :model="fromdata"
        :rules="rules"
        label-width="90px"
        label-position="right"
      >
        <el-row>
          <el-col :span="12">盘点单信息</el-col>
        </el-row>
        <el-divider content-position="center"></el-divider>
        <el-row>
          <el-col :span="6">
            <el-form-item label="盘点单号:">
              <el-input clearable readonly v-model="fromdata.code" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="盘点门店:" prop="storeId">
              <Job-shop
                v-model="fromdata.storeId"
                :defaultStoreId="true"
                placeholder="盘点门店"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="盘点仓库:" prop="storageId">
              <el-select
                clearable
                style="width: 200px"
                v-model="fromdata.storageId"
                placeholder="仓库"
              >
                <el-option
                  v-for="item in storageList"
                  :key="item.id"
                  :value="item.id"
                  :label="item.storageName"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="盘点仓位:" prop="storageSiteId">
              <el-select
                clearable
                f
                style="width: 200px"
                v-model="fromdata.storageSiteId"
                placeholder="仓位"
              >
                <el-option
                  v-for="item in positionList"
                  :key="item.id"
                  :value="item.id"
                  :label="item.storageSiteName"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="单据日期:" prop="receiptsTime" label-width="150">
              <el-date-picker
                value-format="yyyy-MM-dd hh:mm:ss"
                v-model="fromdata.receiptsTime"
                type="datetime"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <el-form-item label="盘点工时:" prop="workHours">
              <el-input clearable v-model.number="fromdata.workHours" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="盘点备注:" prop="remark">
              <el-input
                type="textarea"
                :rows="6"
                placeholder=""
                v-model="fromdata.remark"
                style="width: 600px"
              >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </template>
    <template #contentPanel>
      <el-form
        :inline="true"
        ref="fromdata1"
        :model="fromdata"
        :rules="rules"
        label-width="90px"
        label-position="right"
        class="insuranceForm"
      >
        <el-row>
          <el-col :span="2">盘点人员</el-col>
          <el-col :span="12">
            <el-radio-group v-model="fromdata.userType">
              <el-radio-button :label="false" :value="false">单人</el-radio-button>
              <el-radio-button :label="true" :value="true">组</el-radio-button>
            </el-radio-group>
          </el-col>
        </el-row>
        <el-divider content-position="center"></el-divider>
        <el-row>
          <el-col :span="6">
            <el-form-item label="盘点人员:" label-width="120px">
              <el-input clearable v-model="fromdata.userName" @focus="employeeFocus" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="联系电话:" prop="phone" label-width="120px">
              <el-input clearable v-model.number="fromdata.phone" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </template>
    <!-- <template #contentPanel2>
      <el-row>
        <el-col :span="21">盘点配件列表</el-col>
        <el-col :span="2">
          <el-button icon="el-icon-plus" type="primary" @click="addInventory"
            >添加盘点配件</el-button
          >
        </el-col>
      </el-row>
      <el-divider content-position="center"></el-divider>
      <el-row>
        <data-table
          :value="tableData"
          :total-count="totalCount"
          :has-action="true"
          :table-column-attributes="tableColumnAttributes"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :width="160"
        >
          <template #action="{ row }">
            <i
              class="el-icon-delete"
              title="编辑"
              style="color: red; margin-left: 20px; cursor: pointer"

              @click="handleDataUpdateEdit(row)"
            ></i>
          </template>
        </data-table>
      </el-row>
    </template> -->
    <template #contentPanel3>
      <el-row>
        <el-col :span="23" align="right" justify="center">
          <el-button type="success" @click="saveUpdate">新增盘点单</el-button>
        </el-col>
      </el-row>
    </template>
    <template #dialog>
      <!-- <SelectAccessories v-if="showSelect" /> -->
      <EmployeeTable
        v-if="showEmployeeTable"
        @roleIdListSelectCheck="employeListSelectCheck"
        :checkList="[fromdata.manageUserName]"
      />
    </template>
  </base-layout>
</template>
<script>
import baseMixin from "@/mixins/base";
import { parseTimeToYMDHMS } from "@/utils";
import {
  add,
  update,
  getFindById,
  getAllotCode,
} from "@/api/after-sale/storehouseManage/inventoryCheck";
import { findByStore } from "@/api/after-sale/storehouseManage/warehouseManage";
import EmployeeTable from "../../systemManage/store/components/EmployeeTable";
import SelectAccessories from "@/components/SelectAccessories/index"; //导入组件
import {
  findByStoreIdAndStorageId,
  getDetail as getFindPosDetail,
} from "@/api/after-sale/storehouseManage/positionManage";
import store from "@/store";
export default {
  components: {
    EmployeeTable,
    SelectAccessories,
  },
  mixins: [
    baseMixin({
      addData: add,
      updateData: update,
      format(res) {
        return res.map((data) => {
          return data;
        });
      },
    }),
  ],
  data() {
    return {
      fromdata: {
        id: "",
        code: "",
        userType: false,
        receiptsTime: parseTimeToYMDHMS(new Date()),
      },
      rules: {},
      dict: [],
      showSelect: false, // 是否展示配件组件
      showEmployeeTable: false, //  是否展示员工组件
      storageList: [],
      positionList: [],
      // tableColumnAttributes: [
      //   { name: "配件编号", value: "storageSiteName", type: "text" },
      //   { name: "配件名称", value: "storageSiteName", type: "text" },
      //   { name: "配件类别", value: "storageSiteName", type: "text" },
      //   { name: "配件单位", value: "storageSiteName", type: "text" },
      //   { name: "配件仓位", value: "storageSiteName", type: "text" },
      //   { name: "仓位编号", value: "storageSiteCode", type: "text" },
      //   { name: "库存数量", value: "num", type: "text" },
      //   { name: "现有库存", value: "alreadyNum", type: "text" },
      //   { name: "可增加库存", value: "canNum", type: "text" },
      //   { name: "采购成本", value: "purchasePrice", type: "text" },
      //   { name: "销售价", value: "storagePrice", type: "text" },
      //   { name: "总金额", value: "amount", type: "text" },
      // ],
      rules: {
        receiptsTime: [{ required: true, message: "请选择单据日期", trigger: "change" }],
        storageId: [{ required: true, message: "请选择盘点仓库", trigger: "blur" }],
      },
    };
  },
  watch: {
    "fromdata.storeId": function (value) {
      if (value) {
        this.getWareHouse(value);
      } else {
        this.getWareHouse(store.state.user.entryStore);
      }
    },
    "fromdata.storageId": function (value) {
      if (value) {
        this.findByStoreIdAndStorageId(this.fromdata.storeId, value);
      }
    },
  },
  methods: {
    // 获取仓库
    async getWareHouse(id) {
      let { data } = await findByStore(id);
      this.storageList = data;
    },
    saveUpdate() {
      this.$refs["fromdata"].validate((valid) => {
        if (valid) {
          this.handleDataUpdate(this.fromdata);
          this.closeRouter();
        }
      });
    },
    // 打开配件选择
    addInventory() {
      this.showSelect = true;
      this.showEmployeeTable = false;
      this.handleDataInsert("配件选择");
    },
    //打开员工选择列表
    employeeFocus() {
      this.showSelect = false;
      this.showEmployeeTable = true;
      this.handleDataInsert("管理人员列表");
    },
    // 获取仓位信息
    async findByStoreIdAndStorageId(storeId, storageId) {
      let { data } = await findByStoreIdAndStorageId(storeId, storageId);
      data.forEach((e) => {
        e.value = e.storageSiteName;
      });
      this.positionList = data;
    },
    //员工选择
    employeListSelectCheck(select) {
      this.fromdata.userName = select[0].userName;
      this.fromdata.phone = select[0].phone;
      this.fromdata.userId = select[0].id;
      this.dialogClose();
    },
    async detail(id) {
      let { data } = await getFindById(id);
      this.fromdata = data;
    },
    async getAllotCode() {
      let { data } = await getAllotCode();
      this.fromdata.code = data;
    },
  },
  mounted() {
    let id = this.$route.query.id;
    if (id) {
      this.fromdata.id = id;
      this.detail(id);
    } else {
      this.getAllotCode();
    }
  },
};
</script>
<style lang="scss" scoped></style>
